package com.heima.search.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.dto.UserThreadLocalUtil;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : wuqiang
 * @date : 2022年10月23日
 */
@Service
public class IApUserSearchServiceImpl implements IApUserSearchService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    @Async
    public void saveSearchRecord(String keyWord, User user, String equipmentId) {
        //根据用户id或者设备id and 关键词  查询MongoDB该条数据是否存在
        Query query = null;
        ApUserSearch userSearch = null;
        if (user.getUserId() == 0) {
            //根据设备号查询
            query = new Query(Criteria.where("keyword").is(keyWord).and("equipmentId").is(equipmentId));
            userSearch.setEquipmentId(equipmentId);

        } else {
            query = new Query(Criteria.where("keyword").is(keyWord).and("userId").is(user.getUserId()));
        }
        userSearch = mongoTemplate.findOne(query, ApUserSearch.class);
        if (userSearch == null) {
            userSearch = new ApUserSearch();
            userSearch.setUserId(user.getUserId());
            userSearch.setKeyword(keyWord);
            userSearch.setCreatedTime(new Date());
            mongoTemplate.save(userSearch);
        }
    }

    @Override
    public ResponseResult loadHistory(ApArticleSearchDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getSize() < 0) {
            dto.setSize(1);
        }
        if (dto.getSize() < 0) {
            dto.setSize(20);
        }
        //查询当前登录用户
        User user = UserThreadLocalUtil.get();
        Query query = null;
        if (user.getUserId() == 0) {
            //未登录    根据设备号查询
            query = new Query(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        } else {
            query = new Query(Criteria.where("userId").is(user.getUserId()));

        }
        query.limit(dto.getSize());
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));
        List<ApUserSearch> apUserSearches = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult delHistory(ApArticleSearchDto dto) {
        if (dto == null || ObjUtil.isEmpty(dto.getHisList())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        List<ApUserSearch> hisList = dto.getHisList();

        List<String> idList = hisList.stream().map(apUserSearch -> {
            return apUserSearch.getId();
        }).collect(Collectors.toList());
        //deleter from ApUserSearch where id in(1,2,3,4,5)
        Query query = new Query(Criteria.where("_id").in(idList));
        mongoTemplate.remove(query,ApUserSearch.class);
        return ResponseResult.okResult();
    }
}
